function shapeT = getTargetShape(I, shape, range)
[y x] = size(shape);
[yi xi] = size(I);
NormalList = shape;
shapeT = shape;
for i = 1:x
    P = [shape(1,i) shape(2,i)]';
    if i==1
        P_pre = [shape(1,x) shape(2,x)]';
        P_next = [shape(1,i+1) shape(2,i+1)]';
    elseif i==x
        P_pre = [shape(1,i-1) shape(2,i-1)]';
        P_next = [shape(1,1) shape(2,1)]';
    else
        P_pre = [shape(1,i-1) shape(2,i-1)]';
        P_next = [shape(1,i+1) shape(2,i+1)]';
    end
    
    v1 = (P-P_pre)/norm(P-P_pre);
    v2 = (P_next - P)/norm(P_next - P);
    T = v1 + v2/norm(v1+v2);
    
    Normal = ([-T(2,1) T(1,1)]');
    Normal = -Normal/norm(Normal);
    NormalList(:,i) = Normal;
    
   %search along the normal direction
%   nV = zeros(1,range*2+1);
   stepsize = range/5.0;
   max=0;
   jmax=0;
   for j = -range :stepsize: range
    % hold on;       
     pos = P + j*Normal;
    % plot(round(pos(1)), round(pos(2)),'r');
    % hold off;

     value = texture2D( I,(pos(2,1))/double(yi),(pos(1,1))/double(xi) );
     if(value>max)
         max = value;
         jmax = j;
     end         
   end
   
   shapeT(:,i) = P + (jmax)*Normal;
   
end
hold on;
%quiver(shape(1,:), shape(2,:), NormalList(1,:),NormalList(2,:));
hold off;
            
end
